package com.hl.qzzpbackend.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hl.qzzpbackend.exception.BusinessException;
import com.hl.qzzpbackend.exception.ErrorCode;
import com.hl.qzzpbackend.exception.ThrowUtils;
import com.hl.qzzpbackend.model.dto.resumeInfo.ResumeInfoAddRequest;
import com.hl.qzzpbackend.model.dto.resumeInfo.ResumeInfoUpdateRequest;
import com.hl.qzzpbackend.model.entity.ResumeInfo;
import com.hl.qzzpbackend.model.entity.User;
import com.hl.qzzpbackend.model.enums.EducationalEnum;
import com.hl.qzzpbackend.model.enums.SchoolRankEnum;
import com.hl.qzzpbackend.model.enums.SchoolTypeEnum;
import com.hl.qzzpbackend.model.vo.ResumeInfoVO;
import com.hl.qzzpbackend.service.ResumeInfoService;
import com.hl.qzzpbackend.mapper.ResumeInfoMapper;
import com.hl.qzzpbackend.service.UserService;
import org.hibernate.validator.constraints.pl.REGON;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;


/**
 * 简历信息 业务层
 * @author helong
 */
@Service
public class ResumeInfoServiceImpl extends ServiceImpl<ResumeInfoMapper, ResumeInfo>
    implements ResumeInfoService{

    @Resource
    private UserService userService;

    /**
     * 添加简历信息
     * @param resumeInfoAddRequest 添加简历信息请求类
     * @return 是否成功
     */
    @Override
    public boolean resumeInfoAdd(ResumeInfoAddRequest resumeInfoAddRequest) {
        Long userId = resumeInfoAddRequest.getUserId();
        Integer schoolType = resumeInfoAddRequest.getSchoolType();
        Integer educational = resumeInfoAddRequest.getEducational();
        Integer schoolRank = resumeInfoAddRequest.getSchoolRank();
        List<String> skill = resumeInfoAddRequest.getSkill();
        SchoolTypeEnum schoolTypeEnum = SchoolTypeEnum.getEnumByValue(schoolType);
        EducationalEnum educationalEnum = EducationalEnum.getEnumByValue(educational);
        SchoolRankEnum schoolRankEnum = SchoolRankEnum.getEnumByValue(schoolRank);

        // 参数校验
        ThrowUtils.throwIf(schoolTypeEnum == null, ErrorCode.PARAMS_ERROR,"学校类型错误");
        ThrowUtils.throwIf(educationalEnum == null, ErrorCode.PARAMS_ERROR,"学历错误");
        ThrowUtils.throwIf(schoolRankEnum == null, ErrorCode.PARAMS_ERROR,"学校级别错误");

        long count = userService.lambdaQuery().eq(User::getId, userId).count();
        ThrowUtils.throwIf(count == 0, ErrorCode.PARAMS_ERROR,"用户不存在");
        long count1 = this.lambdaQuery().eq(ResumeInfo::getUserId, userId).count();
        ThrowUtils.throwIf(count1 > 0, ErrorCode.PARAMS_ERROR,"用户已存在简历信息");

        ResumeInfo resumeInfo = new ResumeInfo();
        BeanUtil.copyProperties(resumeInfoAddRequest, resumeInfo);
        String skillStr = JSONUtil.toJsonStr(skill);
        resumeInfo.setSkill(skillStr);

        boolean result = this.save(resumeInfo);
        if(!result){
            throw new BusinessException(ErrorCode.OPERATION_ERROR, "添加简历信息失败");
        }
        return true;
    }

    /**
     * 修改简历信息
     * @param resumeInfoUpdateRequest 修改简历信息请求类
     * @return 是否成功
     */
    @Override
    public boolean resumeInfoUpdate(ResumeInfoUpdateRequest resumeInfoUpdateRequest) {
        Long id = resumeInfoUpdateRequest.getId();
        Long userId = resumeInfoUpdateRequest.getUserId();
        Integer schoolType = resumeInfoUpdateRequest.getSchoolType();
        Integer educational = resumeInfoUpdateRequest.getEducational();
        Integer schoolRank = resumeInfoUpdateRequest.getSchoolRank();
        List<String> skill = resumeInfoUpdateRequest.getSkill();
        SchoolTypeEnum schoolTypeEnum = SchoolTypeEnum.getEnumByValue(schoolType);
        EducationalEnum educationalEnum = EducationalEnum.getEnumByValue(educational);
        SchoolRankEnum schoolRankEnum = SchoolRankEnum.getEnumByValue(schoolRank);

        // 参数校验
        ThrowUtils.throwIf(schoolTypeEnum == null, ErrorCode.PARAMS_ERROR,"学校类型错误");
        ThrowUtils.throwIf(educationalEnum == null, ErrorCode.PARAMS_ERROR,"学历错误");
        ThrowUtils.throwIf(schoolRankEnum == null, ErrorCode.PARAMS_ERROR,"学校级别错误");
        long count = userService.lambdaQuery().eq(User::getId, userId).count();
        ThrowUtils.throwIf(count == 0, ErrorCode.PARAMS_ERROR,"用户不存在");
        long count1 = this.lambdaQuery().eq(ResumeInfo::getId, id).count();
        ThrowUtils.throwIf(count1 == 0, ErrorCode.PARAMS_ERROR,"简历信息不存在");


        ResumeInfo resumeInfo = new ResumeInfo();
        BeanUtil.copyProperties(resumeInfoUpdateRequest, resumeInfo);
        String skillStr = JSONUtil.toJsonStr(skill);
        resumeInfo.setSkill(skillStr);

        boolean result = this.save(resumeInfo);
        if(!result){
            throw new BusinessException(ErrorCode.OPERATION_ERROR, "修改简历信息失败");
        }
        return true;
    }


    /**
     * 获取简历信息
     * @param loginUser 登录用户
     * @return 简历信息
     */
    @Override
    public ResumeInfoVO getResumeInfo(User loginUser) {
        Long userId = loginUser.getId();

        ResumeInfo resumeInfo = this.getById(userId);
        ThrowUtils.throwIf(resumeInfo == null, ErrorCode.PARAMS_ERROR,"简历信息不存在");

        ResumeInfoVO resumeInfoVO = new ResumeInfoVO();
        BeanUtil.copyProperties(resumeInfo, resumeInfoVO);
        List<String> skill = JSONUtil.toList(resumeInfo.getSkill(), String.class);
        resumeInfoVO.setSkill(skill);
        return resumeInfoVO;
    }
}




